import axios from 'axios'
import React, { useEffect, useState } from 'react'
import { notification } from 'antd';
function usePublish(type) {
    const [dataSource, setdataSource] = useState([])
    const { username } = JSON.parse(localStorage.getItem('token'))
    useEffect(() => {
        query()
    }, [username, type])
    const query = () => {
        axios.get(`/news?author=${username}&publishState=${type}&_expand=category`).then(res => {
            setdataSource(res.data)
        })
    }
    const handelUnpublish = (id) => {//下线
        axios.patch(`/news/${id}`, {
            publishState: 3,
        }).then(res => {
            query()
            notification.error({
                message: '通知',
                description: `您可以通过[发布管理/已下线]中查看你的新闻`,
                placement: 'bottomRight'
            })
        })
    }
    const handelPublish = (id) => {//发布
        axios.patch(`/news/${id}`, {
            publishState: 2,
            publishTime: Date.now()
        }).then(res => {
            query()
            notification.success({
                message: '通知',
                description: `您可以通过[发布管理/已发布]中查看你的新闻`,
                placement: 'bottomRight'
            })
        })
    }
    const handelDelete = (id) => {
        axios.delete(`/news/${id}`).then(res => {
            query()
            notification.info({
                message: '通知',
                description: `您已经删除了你的已下线新闻`,
                placement: 'bottomRight'
            })
        })
    }
    return {
        dataSource,
        handelUnpublish,
        handelPublish,
        handelDelete
    }
}

export default usePublish